import 'package:flutter/material.dart';
import './selectWidget.dart';

class SelectAnimation extends StatefulWidget{
  const SelectAnimation({
    Key key,
    this.width = 200,
    this.height = 300,
    this.lists,
    this.tapCb
  }):super(key:key);
  final Function tapCb;
  final double width;
  final double height;
  final List <String> lists;
   @override
  _SelectAnimation createState() => _SelectAnimation(
    width: width,
    lists: lists,
    height: height,
    tapCb: tapCb
  );
}

class _SelectAnimation extends State<SelectAnimation> with TickerProviderStateMixin {
  _SelectAnimation({
    this.width = 200,
    this.height = 20,
    this.lists,
    this.tapCb
  });
  AnimationController controller;
  Function tapCb;
  double width;
  double height;
  List <String> lists;

  @override
  void initState() {
    super.initState();
     controller = AnimationController(
        duration: const Duration(milliseconds: 500), 
        vsync: this
    );
  }

  String tip = '下拉';
  String result = '暂无选择';
  void onPress () {
    if (tip == '下拉') {
      setState(() => tip = '收起');
      controller.forward();
    } else {
      setState(() => tip = '下拉');
      controller.reverse();
    }
  }
  void hideText (String t) {
    tapCb(t);
    setState((){
      tip = '下拉';
      result= t;
    });
    controller.reverse();
  }
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: 40,
      child: Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          FlatButton(
            child: Text(tip),
            onPressed: onPress,
          ),
          SelectList(lists: lists, height: height, width: width, animation: controller, tapCb: hideText),
          Container(
            child: Text(result),
          )
        ],
      ),
    );
  }
}